package com.baidu.controller;

import com.baidu.model.entity.PhotoModelWithBLOBs;
import com.baidu.response.ResponseBody;
import com.baidu.service.impl.PhotoControllerServiceImpl;
import com.until.pagination.PaginationRequest;
import com.until.response.RestfulCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.OutputStream;

/**
 * 图片博客相关的controller 相关接口内容
 * @author user
 */
@RestController
@RequestMapping("photo")
public class PhotoController {
    @Autowired
    private PhotoControllerServiceImpl userControllerService;

    @Autowired
    private ResponseBody responseBody;

    @PostMapping(value ="")
    public RestfulCode.CodeSatCode photoUpload(@RequestParam(required = false) Integer orders,@RequestParam(required = false)  Integer albumId,@RequestParam(required = false)  String name,@RequestParam MultipartFile file){
        try {
            return userControllerService.phoneUpLoad(orders,albumId,name,file,new PhotoModelWithBLOBs());
        } catch (Exception e) {
            return responseBody.USER_ACTION_FAILED;
        }
    }

    @GetMapping("{id}/image")
    public Object getPhotoById(@PathVariable Integer id, OutputStream outputStream){
        try {
            userControllerService.getPhotoBilbo(id,outputStream);
        } catch (Exception e) {
            return responseBody.NOT_FOUND_EXAMPLE;
        }
        return null;
    }

    @GetMapping("{id}")
    public RestfulCode.CodeSatCode getPhotoBilbo(@PathVariable Integer id, HttpServletRequest request){
        try {
            return userControllerService.getPhotoBilboInfo(id,request);
        } catch (Exception e) {
            return responseBody.NOT_FOUND_EXAMPLE;
        }
    }

    @GetMapping("user/{id}")
    public RestfulCode.CodeSatCode getPhotoBilboList(@PathVariable Integer id,
                                                     @RequestParam(value = "orders",required = false,defaultValue ="1") Integer orders,
                                                     @ModelAttribute PaginationRequest page,
                                                     HttpServletRequest request){
        return userControllerService.getPhotoBilboInfoByAlbumId(id,orders,request,page);
    }

    @DeleteMapping("{id}")
    public RestfulCode.CodeSatCode deletePhotoById(@PathVariable Integer id){
        return userControllerService.deletePhotoById(id);
    }
}
